package org.jeecg.modules.lab.documents.controller;

import java.util.Arrays;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecg.modules.lab.documents.entity.LabDocuments;
import org.jeecg.modules.lab.documents.management_technology.lab_countDoc.controller.LabCountDocController;
import org.jeecg.modules.lab.documents.management_technology.lab_countDoc.entity.LabCountDoc;
import org.jeecg.modules.lab.documents.service.ILabDocumentsService;
import org.jeecg.common.system.base.controller.JeecgController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.apache.shiro.authz.annotation.RequiresPermissions;

 /**
 * @Description: 文档中心
 * @Author: jeecg-boot
 * @Date:   2024-01-22
 * @Version: V1.0
 */
@Api(tags="文档中心")
@RestController
@RequestMapping("/documents/labDocuments")
@Slf4j
public class LabDocumentsController extends JeecgController<LabDocuments, ILabDocumentsService> {
	@Autowired
	private ILabDocumentsService labDocumentsService;
	@Autowired
	private LabCountDocController labCountDocController;
	/**
	 * 分页列表查询
	 *
	 * @param labDocuments
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	//@AutoLog(value = "文档中心-分页列表查询")
	@ApiOperation(value="文档中心-分页列表查询", notes="文档中心-分页列表查询")
	@GetMapping(value = "/list")
	public Result<IPage<LabDocuments>> queryPageList(LabDocuments labDocuments,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<LabDocuments> queryWrapper = QueryGenerator.initQueryWrapper(labDocuments, req.getParameterMap());
		Page<LabDocuments> page = new Page<LabDocuments>(pageNo, pageSize);
		IPage<LabDocuments> pageList = labDocumentsService.page(page, queryWrapper);
		return Result.OK(pageList);
	}

	/**
	 *   添加
	 *
	 * @param labDocuments
	 * @return
	 */
	@AutoLog(value = "文档中心-添加")
	@ApiOperation(value="文档中心-添加", notes="文档中心-添加")
	//@RequiresPermissions("lab_documents:lab_documents:add")
	@PostMapping(value = "/add")
	public Result<String> add(@RequestBody LabDocuments labDocuments) {
		LabCountDoc labCountDoc = new LabCountDoc();
		labCountDoc.setManagements("管理类");
		labCountDoc.setDocType(labDocuments.getDocType());
		labCountDoc.setNumber(labDocuments.getNumber());
		labCountDoc.setUploadFiles(labDocuments.getUploadFiles());
		String s = labDocuments.getUploadFiles().replaceAll(",temp/", "/");
		String s1 = s.replaceAll("temp/", "");
		labDocuments.setDisplayName(s1);
		labCountDocController.add(labCountDoc);
		labDocuments.setManagements("管理类");
		labDocumentsService.save(labDocuments);
		return Result.OK("添加成功！");
	}

	/**
	 *  编辑
	 *
	 * @param labDocuments
	 * @return
	 */
	@AutoLog(value = "文档中心-编辑")
	@ApiOperation(value="文档中心-编辑", notes="文档中心-编辑")
	//@RequiresPermissions("lab_documents:lab_documents:edit")
	@RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
	public Result<String> edit(@RequestBody LabDocuments labDocuments) {
		String s = labDocuments.getUploadFiles().replaceAll(",temp/", "/");
		String s1 = s.replaceAll("temp/", "");
		labDocuments.setDisplayName(s1);
		labDocumentsService.updateById(labDocuments);
		return Result.OK("编辑成功!");
	}

	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "文档中心-通过id删除")
	@ApiOperation(value="文档中心-通过id删除", notes="文档中心-通过id删除")
	//@RequiresPermissions("lab_documents:lab_documents:delete")
	@DeleteMapping(value = "/delete")
	public Result<String> delete(@RequestParam(name="id",required=true) String id) {
		labDocumentsService.removeById(id);
		return Result.OK("删除成功!");
	}

	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "文档中心-批量删除")
	@ApiOperation(value="文档中心-批量删除", notes="文档中心-批量删除")
	//@RequiresPermissions("lab_documents:lab_documents:deleteBatch")
	@DeleteMapping(value = "/deleteBatch")
	public Result<String> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.labDocumentsService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}

	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	//@AutoLog(value = "文档中心-通过id查询")
	@ApiOperation(value="文档中心-通过id查询", notes="文档中心-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<LabDocuments> queryById(@RequestParam(name="id",required=true) String id) {
		LabDocuments labDocuments = labDocumentsService.getById(id);
		if(labDocuments==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(labDocuments);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param labDocuments
    */
    @RequiresPermissions("lab_documents:lab_documents:exportXls")
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, LabDocuments labDocuments) {
        return super.exportXls(request, labDocuments, LabDocuments.class, "文档中心");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @RequiresPermissions("lab_documents:lab_documents:importExcel")
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, LabDocuments.class);
    }

}
